#include "ST.h"


void ST_init(ST* pst)
{
    assert(pst);
    pst->pdata = NULL;
    pst->top = 0;
    pst->capacity = 0;
}
void ST_destroy(ST* pst)
{
    assert(pst);
    free(pst->pdata);
    pst->pdata = NULL;
    pst->top = 0;
    pst->capacity = 0;
}
void ST_PUSH(ST* pst,STDataType x)
{
    assert(pst);
    if(pst->top +1  >= pst->capacity)
    {
        STDataType* newp =(STDataType*)realloc(pst->pdata,sizeof((1+2*pst->capacity)*sizeof(STDataType)));
        if(!newp)
        {
            perror("malloc fail:\n");
            return ;
        }
        pst->capacity = pst->capacity*2+1;
        pst->pdata = newp;
        pst->pdata[(pst->top)++] = x;
    }
    else
    {
        pst->pdata[(pst->top)++] = x;
    }
    return ;
}
void ST_POP(ST* pst)
{
    assert(pst);
    assert(!ST_empty(pst));
    pst->top--;

}
STDataType ST_TTOP(ST* pst)//get the data int the top
{
    assert(pst);
    assert(!ST_empty(pst));
    return pst->pdata[pst->top-1];
}
bool ST_empty(ST* pst)
{
    assert(pst);
    if(pst->top<=0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int ST_size(ST* pst)
{
    assert(pst);
    return pst->capacity;   
}
